home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!usenet
- From: GHouck <hksys@teleport.com>
- Newsgroups: comp.lang.c
- Subject: Re: Q: Newbie using text files
- Date: 20 Apr 1996 20:14:35 GMT
- Organization: systems hk
- Message-ID: <4lbgjb$l8u@nadine.teleport.com>
- References: <4l6aqu$rfr@chile.it.earthlink.net>
- NNTP-Posting-Host: ip-pdx03-10.teleport.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- brunop@earthlink.net (Peter Bruno) wrote:
- >I'm new to C programming so bear with me for a bit...
- >
- >I do a lot of small programming projects at work, mostly to make my
- >life easier, and I've been using QBasic. I would like, however, to
- >use a betting langauage if I could. But, trying to input and
- >processes text files in C seems a lot harder than in C, what with
- >pointers and all.
- >
- >My point, and I do have one, is it worth my time to learn C to process
- >ASCII text files with fixed lenth fields, for sorting etc...
- >
- >and could you suggest some routines for doing this... in basic I just
- >do the following:
- >
- >open "foo.bar" for input as #1
- >open "foo.out" for output as #2
-
- >while not eof(1)
- > line input #1, row
- > FName = mid$(row, 1, 25) 'characters 1-25 are first name
- > LName = mid$(row, 26, 50)
- > test = mid$(LName, 26, 1)
- > if test < "M" then print #2, row 'if person's last name begins with
- >wend
-
- >close #1, #2
- >
- >can someone (kind-hearted and forgiving of stupid questions) please
- >offer suggestions on how to do this in C? PLEASE!
- >
- Peter,
-
- The following (without any error-trapping) is a pretty close
- analog to your BASIC code. So close that it ought to convince
- you that you many of the rules and techniques are similar
- if you need them to be.
-
- Yours, Geoff Houck
-
- int main( void )
- {
- char row[128], fname[64] lname[64];
- FILE *f1, *f2;
-
- f1 = fopen( "foo.bar","r" );
- f2 = fopen( "foo.out","w" );
-
- while( fgets(row,sizeof(row),f1) ) {
- strncpy( fname,row,25 );
- fname[25] = '\0';
- strncpy( lname,(row+25),25 );
- lname[25] = '\0';
-
- if( *lname < 'M' )
- fprintf( f2,"%s",row );
- }
-
- fclose( f1 );
- fclose( f2 );
- }
-
-
-